package ui.forms;

import com.sun.lwuit.*;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import utils.Config;

/**
 *
 * @author Shayan
 */
public class ChangeServerForm extends Form implements ActionListener {

    private Label lblServerAddress;
    private TextArea txtServerAddress;
    private Button btnChange;
    private Command cmdBack;
    private Form parent;
    private Container mainContainer;

    public ChangeServerForm(Form parent) {
        this.parent = parent;

        setTitle("Change server address");
        int w = this.getPreferredW();

        mainContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS));

        lblServerAddress = new Label("Enter server address:");
        txtServerAddress = new TextArea();

        txtServerAddress.setConstraint(TextArea.URL);
        txtServerAddress.setText(Config.getServerAddress());
        txtServerAddress.setPreferredW(w);

        btnChange = new Button("Change");
        btnChange.setAlignment(CENTER);
        btnChange.setPreferredW(w / 4);
        btnChange.addActionListener(this);

        cmdBack = new Command("Back");

        this.setBackCommand(cmdBack);
        this.addCommandListener(this);

        this.addCommand(cmdBack);
        mainContainer.addComponent(lblServerAddress);
        mainContainer.addComponent(txtServerAddress);

        this.addComponent(mainContainer);
        this.addComponent(btnChange);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getCommand() == cmdBack) {
            parent.showBack();
        }
        if (e.getSource() == btnChange) {
            try {
                Config.setServerAddress(txtServerAddress.getText());
                Dialog.show("Successful", "Server address saved", "OK", "");
            } catch (Exception ex) {
                Dialog.show("Error", "Error:" + ex.toString(), "OK", "");
            }
        }
    }
}
